class AddPatient < ActiveRecord::Migration
  def self.up
    
    hospicebranch = HospiceBranch.find(:first)
    
    @parsed_file = CSV::Reader.parse(File.open('db/testData/patients.csv','r'))
    @parsed_file.each do |row|
      patient = Patient.new
      patient.hospice_branch_id = hospicebranch.id
      patient.last_name = row[1]
      patient.title = row[2]
      patient.initials = row[3]
      patient.first_name = row[4]
      patient.res_address_1 = row[5]
      patient.res_address_2 = row[6]
      patient.res_address_post_code = row[9]
      patient.post_address_1 = row[10]
      patient.post_address_2 = row[11]
      patient.post_address_post_code = row[14]
      patient.home_telephone_number = row[15]
      patient.cellphone_number = row[16]
      patient.identity_number = row[17]
      if row[18].nil? && row[18].to_s.empty?
        patient.birth_date = "1901-01-01"
      else
        patient.birth_date = row[18]
      end

      patient.gender = row[20]
      
      marital_status = MaritalStatus.find(:first, :conditions => {:status_name => row[21]})
      if marital_status.nil?
        marital_status = MaritalStatus.create(:status_name => row[21])
      end
      patient.marital_status_id = marital_status.id
      
      race = Race.find(:first, :conditions => {:race_description => row[22]})
      if race.nil?
        race = Race.create(:race_description => row[22])
      end
      patient.race_id = race.id
      
      religion = Religion.find(:first, :conditions => {:religion_name => row[23]})
      if religion.nil?
        religion = Religion.create(:religion_name => row[23])
      end
      patient.religion_id = religion.id
      
      medicalaid = MedicalAid.find(:first, :conditions =>{:medical_aid_name => row[24]})
      if medicalaid.nil?
        medicalaid = MedicalAid.create(:medical_aid_name => row[24])
      end
      patient.medical_aid_id = medicalaid.id
      
      patient.medical_aid_number = row[25]
      
      medicalinsurer = MedicalInsurer.find(:first, :conditions => {:medical_insurer_name => row[26]})
      if medicalinsurer.nil?
        medicalinsurer = MedicalInsurer.create(:medical_insurer_name => row[26])
      end
      patient.medical_insurance_id = medicalinsurer.id
      patient.medical_insurance_number = row[27]
      
      undertaker = Undertaker.find(:first, :conditions => {:undertaker_name => row[28]})
      if undertaker.nil?
        undertaker = Undertaker.create(:undertaker_name => row[28])
      end
      patient.undertaker_id = undertaker.id
      
      patient.policy_number = row[29]
      
      burialtype = BurialType.find(:first, :conditions => {:burial_type_name => row[30]})
      if burialtype.nil?
        burialtype = BurialType.create(:burial_type_name => row[30])
      end
      patient.burial_type_id = burialtype.id
      
      #50s
      
      patient.employer_name = row[51]
      patient.employer_contact = row[52]
      patient.emp_address_1 = row[53]
      patient.emp_address_2 = row[54]
      patient.emp_address_post_code = row[57]
      patient.employer_telephone = row[58]
      patient.referral_date = row[67]
      patient.admission_date = row[73]
      
      patient.diagnosis_id = 1
      
      patient.save
    end
  end

  def self.down
    Patient.destroy_all 
  end
end
